package cn.itcast.order.service;

import cn.itcast.feign.clients.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        if (order == null) {
            log.warn("order is null");
            return null;
        }
//        String url = "http://userservice/user/" + order.getUserId();
//        log.info("calling user-service url: {}", url);
//        User user = restTemplate.getForObject(url, User.class);
        User user = userClient.findById(order.getUserId());
        if (user != null) {
            log.warn("userId: {}, user: {}", order.getUserId(), user);
        }
        order.setUser(user);
        // 4.返回
        return order;
    }
}
